﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using netDxf.Entities;

namespace CPlatePrintSys.Annotation.Models
{
    public class TextEdittingPanelInfo
    {
        public TextEdittingPanelInfo() { }

        public double X { get; set; } = 0;
        public double Y { get; set; } = 0;

        public bool IsOpen { get; set; } = false;
        
        /// <summary>
        /// 控件ID，用于区分不同的控件实例
        /// </summary>
        public string ControlId { get; set; } = string.Empty;
        
        /// <summary>
        /// 选中的文字内容
        /// </summary>
        public string SelectedText { get; set; } = string.Empty;
        
        /// <summary>
        /// 文字信息
        /// </summary>
        public TextInfo TextInfo { get; set; } = null;
    }
    
    /// <summary>
    /// 文字信息类，用于存储文字的相关信息
    /// </summary>
    public class TextInfo
    {
        /// <summary>
        /// 唯一标识符
        /// </summary>
        public string Id { get; set; }
        
        public string Text { get; set; }
        public Point3D Position { get; set; }
        public double Height { get; set; }
        public double Rotation { get; set; }
        public string LayerName { get; set; }

        public Color Color { get; set; } = Colors.Black;
        
        public bool Underline { get; set; } = false;

        public EntityObject OriginalEntity { get; set; }
        
        /// <summary>
        /// 转换为同步用的简化版本
        /// </summary>
        /// <returns>TextInfoSync对象</returns>
        public TextInfoSync ToSyncInfo()
        {
            return new TextInfoSync
            {
                Id = this.Id,
                Text = this.Text,
                PositionX = this.Position.X,
                PositionY = this.Position.Y,
                PositionZ = this.Position.Z,
                Height = this.Height,
                Rotation = this.Rotation,
                LayerName = this.LayerName,
                ColorR = this.Color.R,
                ColorG = this.Color.G,
                ColorB = this.Color.B,
                ColorA = this.Color.A,
                Underline = this.Underline
            };
        }
        
        /// <summary>
        /// 从同步信息更新当前对象
        /// </summary>
        /// <param name="syncInfo">同步信息</param>
        public void UpdateFromSyncInfo(TextInfoSync syncInfo)
        {
            if (syncInfo == null) return;
            
            this.Text = syncInfo.Text;
            this.Position = new Point3D(syncInfo.PositionX, syncInfo.PositionY, syncInfo.PositionZ);
            this.Height = syncInfo.Height;
            this.Rotation = syncInfo.Rotation;
            this.LayerName = syncInfo.LayerName;
            this.Color = Color.FromArgb(syncInfo.ColorA, syncInfo.ColorR, syncInfo.ColorG, syncInfo.ColorB);
            this.Underline = syncInfo.Underline;
        }
    }
    
    /// <summary>
    /// 文字信息同步类，用于事件传输，只包含基本数据类型
    /// </summary>
    public class TextInfoSync
    {
        /// <summary>
        /// 唯一标识符
        /// </summary>
        public string Id { get; set; }
        
        public string Text { get; set; }
        public double PositionX { get; set; }
        public double PositionY { get; set; }
        public double PositionZ { get; set; }
        public double Height { get; set; }
        public double Rotation { get; set; }
        public string LayerName { get; set; }
        public byte ColorR { get; set; }
        public byte ColorG { get; set; }
        public byte ColorB { get; set; }
        public byte ColorA { get; set; }
        public bool Underline { get; set; }
        
        /// <summary>
        /// 转换为完整的TextInfo对象
        /// </summary>
        /// <returns>TextInfo对象</returns>
        public TextInfo ToTextInfo()
        {
            return new TextInfo
            {
                Id = this.Id,
                Text = this.Text,
                Position = new Point3D(this.PositionX, this.PositionY, this.PositionZ),
                Height = this.Height,
                Rotation = this.Rotation,
                LayerName = this.LayerName,
                Color = Color.FromArgb(this.ColorA, this.ColorR, this.ColorG, this.ColorB),
                Underline = this.Underline
            };
        }
    }
}
